package com.elvin.platform.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.Organize;
import com.elvin.platform.beans.Role;
import com.elvin.platform.service.RoleService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Controller
@RestController
@RequestMapping("/roleController")
@Log4j2
@Api("角色管理")
public class RoleController {
	@Resource
	private RoleService service;
	@ApiIgnore
	@ApiOperation(value = "获取所有角色")
	@PostMapping("/queryAll")
	public Result<Object> queryAll() {
		Result<Object> result = new Result<>();
		try {
			List<Role> selectList = service.selectList(null);
			result.success(selectList);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}
	@ApiOperation(value = "分页获取所有数据")
	@PostMapping("/selectPage")
	public Result<Object> selectForPage(
			@ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false)Integer pageNumber,
			@ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false)Integer pageSize
	) {
		Result<Object> result = new Result<>();
		try {
      int count = service.selectCount(null);
      if (count > 0) {
        List<Role> selectList = new ArrayList<>();
        if (pageNumber != null && pageSize != null) {
          Page<Role> page = new Page<>(pageNumber, pageSize);
          service.selectPage(page, null);
          selectList = page.getRecords();
        } else {
          selectList = service.selectList(null);
        }
        result.success(selectList,count);
      } else {
        result.success(null, count);
      }
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}
	@ApiOperation(value = "新增角色")
	@PostMapping("/insert")
	public Result<Object> insert(@RequestBody Role entity) {
		Result<Object> result = new Result<>();
		try {
			service.insert(entity);
			result.success(entity, "新增成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "修改角色")
	@PostMapping("/update")
	public Result<Object> update(@RequestBody Role entity) {
		Result<Object> result = new Result<>();
		try {
			service.updateById(entity);
			result.success("修改成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "删除角色")
	@PostMapping("/delete")
	public Result<Object> delete(@ApiParam(name = "id" ,value = "角色id",required = true) @RequestParam("id") Integer id) {
		Result<Object> result = new Result<>();
		try {
			service.deleteById(id);
			result.success("删除成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "根据ID获取角色")
	@PostMapping("/getById")
	public Result<Object> getById(@ApiParam(name = "角色id",value = "角色id",required = true) @RequestParam("id") Integer id) {
		Result<Object> result = new Result<>();
		try {
			Role entity = new Role();
			entity.setId(id);
			entity = service.selectById(entity);
			result.success(entity);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}


	@ApiOperation(value = "根据roleId获取角色列表信息")
	@PostMapping("/getRolesByIds")
	public Result<Object> getRolesByIds(@ApiParam(name = "ids",value = "角色id",required = true) @RequestParam("ids") String ids) {
		Result<Object> result = new Result<>();
		try {
			String[] split = ids.split(",");
			java.util.ArrayList<String> stringList = new ArrayList<>(split.length);
		    Collections.addAll(stringList,split);
			List<Role> roles = service.selectBatchIds(stringList);
			result.success(roles);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}
}

